//
//  MasterListVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/7/17.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "MasterListVC.h"
#import "InformationVM.h"
#import "RuleCell.h"
#import "Home_TypeView.h"
#import "PlanRankingCell.h"
#import "MJRefresh.h"
#import "PlanPeopleVC.h"
@interface MasterListVC ()<UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArr;
@property (nonatomic ,strong) Home_TypeView *home_TypeView;
@property (nonatomic ,strong) NSMutableArray *advertisingArr;
@property (nonatomic ,assign) NSInteger   page;


@property (nonatomic ,strong) NSMutableArray *arr1;
@property (nonatomic ,strong) NSMutableArray *arr2;
@property (nonatomic ,strong) NSMutableArray *arr3;
@property (nonatomic ,strong) NSMutableArray *colArr1;
@property (nonatomic ,strong) NSMutableArray *colArr2;
@property (nonatomic ,strong) NSMutableArray *colArr3;


@end

@implementation MasterListVC

-(void)setRuleArr:(NSArray *)ruleArr WithSubRuleArr:(NSArray *)subRuleArr WithselectRule:(NSInteger)selectRule WithSelectSubRule:(NSInteger)selectSubRule{
    
    
    self.ruleArr = ruleArr;
    self.subRuleArr = subRuleArr;
    self.selectRule = selectRule;
    self.selectSubRule = selectSubRule;
    
    [self.collectionView reloadData];
   
    [self.tableView.header beginRefreshing];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.arr1 = [NSMutableArray array];
    self.arr2 = [NSMutableArray array];
    self.arr3 = [NSMutableArray array];
    self.colArr1 = [NSMutableArray array];
    self.colArr2 = [NSMutableArray array];
    self.colArr3 = [NSMutableArray array];


}

//
//-(void)setRuleArr:(NSArray *)ruleArr{
//
//    _ruleArr = ruleArr;
//
//    PlayRuleModel *model = ruleArr.firstObject;
//
//    _lType = [NSString stringWithFormat:@"%ld",model.lType];
//
//
//
//}

//-(void)setSubRuleArr:(NSArray *)subRuleArr{
//    _subRuleArr = subRuleArr;
//    [self.collectionView reloadData];
//
//}

- (void)hiddenCollectionView{
    if (self.subRuleArr.count==1) {
        [_collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@(real_size(0)));
        }];
    }else{
        [_collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@(real_size(50)));
        }];
    }
}

- (void)getData{
    WS(weakSelf);
    PlayRuleModel *model=self.subRuleArr[self.selectSubRule];
    [[InformationVM manage] getMasterListWithLType:[NSString stringWithFormat:@"%ld",model.lType] andPlayName:model.PlayName andPage:weakSelf.page Success:^(id returnValue) {
        weakSelf.page+=1;
        [weakSelf.dataArr addObjectsFromArray:returnValue];
        [weakSelf.tableView.header endRefreshing];
        NSArray *arr=returnValue;
        if (arr.count<20) {
            [weakSelf.tableView.footer noticeNoMoreData];
        }else{
            [weakSelf.tableView.footer endRefreshing];
        }
        [weakSelf.tableView reloadData];
        if (weakSelf.advertisingArr==nil) {
            [weakSelf getAdvertising];
        }
        
    } andFail:^(NSString *errorMessage, NSError *error) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
    }];
}


#pragma mark -- 获取广告
- (void)getAdvertising{
    WS(weakSelf);
    if ([BaseVM manage].city.length>0) {
        [[InformationVM manage] getAdvertWithLocation:@"0" andType:7 Success:^(id returnValue) {
            weakSelf.advertisingArr=returnValue;
            
            
            for (AdvertisingModel *model in self.advertisingArr) {
                
                if (model.Layer == 3) {
                    [self.arr3 addObject:model];
                    if (model.IsDisplay) {
                        [self.colArr3 addObject:model];
                    }
                    
                }
                if (model.Layer == 1) {
                    
                    [self.arr1 addObject:model];
                    if (model.IsDisplay) {
                        [self.colArr1 addObject:model];
                    }
                    
                }
                if (model.Layer == 2) {
                    
                    [self.arr2 addObject:model];
                    if (model.IsDisplay) {
                        [self.colArr2 addObject:model];
                    }
                }
            }
            
            [weakSelf.tableView reloadData];
        } andFail:^(NSString *errorMessage, NSError *error) {
            
        }];
    }
}


#pragma mark - collectionView代理函数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.subRuleArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RuleCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"RuleCell" forIndexPath:indexPath];
    cell.model=self.subRuleArr[indexPath.row];
    cell.isSelected=(indexPath.row==_selectSubRule);
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.dic = nil;
    self.selectSubRule=indexPath.row;
    [collectionView reloadData];
    [self.tableView.header beginRefreshing];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREEN_WIDTH/self.subRuleArr.count, real_size(50));
}

#pragma mark -- UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *tempArr=[NSMutableArray array];
    NSMutableArray *tempColArr=[NSMutableArray array];
    if (indexPath.row==1) {
        tempArr = self.arr1;
        tempColArr = self.colArr1;
    }else if (indexPath.row==3){
         tempArr = self.arr2;
        tempColArr = self.colArr2;

    }else if (indexPath.row==4){
         tempArr = self.arr3;
        tempColArr = self.colArr2;

    }
    
    if (tempArr.count>0) {
        PlanRankingCell *cell=[tableView dequeueReusableCellWithIdentifier:@"PlanRankingCell1" forIndexPath:indexPath];
        cell.model=self.dataArr[indexPath.row];
        cell.modelArr =tempArr;
        [cell setModelArr:tempArr AndRowCount:(NSInteger)(tempArr.count-ceil(tempColArr.count/2.0))];
        return cell;
    }
    else{
        PlanRankingCell *cell=[tableView dequeueReusableCellWithIdentifier:@"PlanRankingCell" forIndexPath:indexPath];
        cell.model=self.dataArr[indexPath.row];
        cell.modelArr =nil;
        return cell;
    }
//
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ExpertsModel *eModel=self.dataArr[indexPath.row];
    PlanPeopleVC *vc=[[PlanPeopleVC alloc]init];
    LotteryTypeModel *lotteryModel=[[LotteryTypeModel alloc]init];
    lotteryModel.lType=eModel.lType;
    vc.lotteryModel=lotteryModel;
    vc.model=eModel;
    PlayRuleModel *model=self.subRuleArr[self.selectSubRule];
    vc.playName=model.PlayName;
    vc.title=[NSString stringWithFormat:@"%@近期竞彩",self.strTitle];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark -- 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        WS(weakSelf);
        _tableView=[[UITableView alloc]init];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=DEFAULTBGCOLOR;
        _tableView.estimatedRowHeight=150.0f;
        
        [_tableView registerClass:[PlanRankingCell class] forCellReuseIdentifier:@"PlanRankingCell"];
        [_tableView registerClass:[PlanRankingCell class] forCellReuseIdentifier:@"PlanRankingCell1"];
        
        _tableView.rowHeight=UITableViewAutomaticDimension;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.page=1;
            [weakSelf.dataArr removeAllObjects];
            [weakSelf.tableView reloadData];
            [weakSelf getData];
        }];
        _tableView.footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getData];
        }];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(weakSelf.view);
            make.top.equalTo(weakSelf.collectionView.mas_bottom);
        }];
        
    }
    return _tableView;
}

- (UICollectionView *)collectionView{
    if (_collectionView==nil) {
        WS(weakSelf);
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake(SCREEN_WIDTH/4.0, SCREEN_WIDTH/4.0)];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor=[UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator=NO;
        [_collectionView registerClass:[RuleCell class] forCellWithReuseIdentifier:@"RuleCell"];
        [self.view addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(weakSelf.view);
            make.height.mas_equalTo(@(real_size(50)));
        }];
    }
    return _collectionView;
}



- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
